home *** CD-ROM | disk | FTP | other *** search
- /* PlotLongArray -------------------------------------------------
- *
- * Plot array of longs.
- *
- * Copyright (c) 1993 Bill Karsh.
- * All rights reserved.
- *
- */
-
-
- #pragma options( honor_register, !assign_registers )
-
-
- #include "PlotLongArray.h"
-
- #define TextMargin 1
-
-
-
-
-
-
- /* PlotLongArray -------------------------------------------------
- *
- * Plot array of N longs.
- *
- * min and max values set the scales.
- * r bounds the entire plot including labels.
- *
- * Copyright (c) 1993 Bill Karsh.
- * All rights reserved.
- *
- */
-
-
- #pragma options( honor_register, !assign_registers )
-
-
- #include "PlotLongArray.h"
-
-
-
-
-
-
- void PlotLongArray(
- register long *data,
- long N,
- long hMin,
- long hMax,
- long vMin,
- long vMax,
- Rect *r,
- StringPtr title )
- {
- register short v0, wHi, hScale, vScale;
- register long i, sum;
- short h, wWid, sMinWid, sMaxWid;
- Rect R;
- Point p;
- Byte sMin[16], sMax[16];
- FontInfo fi;
-
- // adjust vMin and vMax
-
- if( vMin > 0 && vMax > 0 )
- vMin = 0;
- else if( vMin < 0 && vMax < 0 )
- vMax = 0;
-
- R = *r;
- EraseRect( &R );
-
- // make room for labels on left and bottom
-
- NumToString( vMin, sMin );
- NumToString( vMax, sMax );
- GetFontInfo( &fi );
- sMinWid = StringWidth( sMin );
- sMaxWid = StringWidth( sMax );
- h = sMaxWid;
- if( sMinWid > h ) h = sMinWid;
-
- R.left += h + TextMargin*2;
- R.bottom -= fi.ascent + fi.descent + TextMargin;
-
- ForeColor( greenColor );
- FrameRect( &R );
- InsetRect( &R, 1, 1 );
-
- // vert labels
-
- ForeColor( blackColor );
- MoveTo( R.left - sMaxWid - TextMargin,
- R.top + fi.ascent );
- DrawString( sMax );
-
- MoveTo( R.left - sMinWid - TextMargin,
- R.bottom - fi.descent );
- DrawString( sMin );
-
- // horiz labels
-
- v0 = R.bottom + fi.ascent + TextMargin;
- NumToString( hMin, sMin );
- MoveTo( R.left + TextMargin, v0 );
- DrawString( sMin );
-
- NumToString( hMax, sMax );
- MoveTo( R.right - StringWidth( sMax ) - TextMargin, v0 );
- DrawString( sMax );
-
- if( title ) {
- MoveTo((R.right + R.left - StringWidth(title))/2, v0);
- DrawString( title );
- }
-
- // get ready to plot
-
- hScale = hMax - hMin;
- vScale = vMax - vMin;
-
- if( !hScale || !vScale ) return;
-
- wWid = R.right - R.left;
- wHi = R.bottom - R.top;
-
- // draw absissa at v = 0
-
- v0 = R.top + (vMax * wHi) / vScale;
-
- MoveTo( R.left, v0 );
- LineTo( R.right - 1, v0 );
-
- // draw data
-
- sum = 0;
-
- for( i = 0; i < N; i++ ) {
-
- h = R.left + (i * wWid) / hScale;
-
- MoveTo( h, v0 );
- LineTo( h, v0 - (*data * wHi) / vScale );
-
- sum += *data;
- data++;
- }
-
- // draw average v line
-
- v0 -= (sum/N * wHi) / vScale;
-
- ForeColor( redColor );
- MoveTo( R.left, v0 );
- LineTo( R.right - 1, v0 );
-
- ForeColor( blackColor );
- }